import { resultFailure, resultSuccess } from '../utils/resultFormatter'
import { prisma } from './index.repo'

export const createOrganization = async (name: string, description: string) => {
    try {
        const organization = await prisma.organization.create({
            data: {
                name,
                description
            }
        })

        return resultSuccess(true, 201, organization)
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const deleteOrganization = async (orgId: string) => {
    try {
        const organization = await prisma.organization.update({
            where: { id: orgId },
            data: {
                deletedAt: new Date()
            }
        })

        return resultSuccess(true, 200, organization, 'Delete successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const updateOrganization = async (orgId: string, name: string, description: string) => {
    try {
        const result = await prisma.organization.update({
            where: { id: orgId },
            data: { name, description }
        })

        return resultSuccess(true, 200, result, 'Update successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const readOrganization = async (offset: number = 0, limit: number = 10, displayDelete: boolean = false) => {
    try {
        const total = await prisma.organization.count({
            where: {
                deletedAt: displayDelete ? undefined : null
            }
        })
        const organizations = await prisma.organization.findMany({
            skip: offset,
            take: limit,
            where: {
                deletedAt: displayDelete ? undefined : null
            }
        })

        return resultSuccess(true, 200, { organizations, total }, 'Read organizations successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const readOrganizationAdmin = async (orgId: string) => {
    try {
        const result = await prisma.adminOrganization.findMany({
            where: {
                organizationId: orgId,
                deletedAt: null
            },
            include: {
                admin: {
                    select: {
                        id: true,
                        username: true,
                        email: true,
                        phone: true,

                        createdAt: true,
                        updatedAt: true
                    }
                }
            }
        })
        return resultSuccess(true, 200, result)
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const readOrganizationMember = async (orgId: string) => {
    try {
        const result = await prisma.member.findMany({
            where: {
                organizationId: orgId,
                deletedAt: null
            }
        })

        if (!result) return resultFailure(false, 404, 'Organization member not found')

        return resultSuccess(true, 200, result)
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

export const readOrganizationGroup = async (orgId: string) => {
    try {
        const result = await prisma.group.findMany({
            where: {
                organizationId: orgId,
                deletedAt: null
            }
        })

        if (!result) return resultFailure(false, 404, 'Organization group not found')

        return resultSuccess(true, 200, result)
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}
